package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.util.FileNames;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Tool;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements NavService {
    //    private File file = null;
//    private String split = "\n";

    @Override
    public NavModel[] getNavModels() {

/**
 * 1.创建文件
 * 2.写入数据
 * 3.读取数据
 * 4.封装数据
 *
 */
//1.创建文件
//        String fileName = "nav.txt";
        IOTool.createFile(FileNames.FILE_NAME_NAV);
//        2.写入数据
        List<String> data = getDaoData();
        IOTool.writeData(data);
//3.读取数据
        String result = IOTool.readData();
//        4.封装数据
//        NavModel[] navModels = setData(result);
        return setData(result);
    }

    private List<String> getDaoData() {
        NavDao navDao = new NavDao();
        List<String> list = new ArrayList<>();
        list.add(Arrays.toString(navDao.hrefs));
        list.add(Arrays.toString(navDao.titles));
        return list;
    }

    private NavModel[] setData(String result) {
//        NavDao navDao = new NavDao();
        System.out.println(result);
        String[] results = result.split(FileNames.SPLIT);
        String href = results[0];
//        去掉中括号？
//        1.截取
//        2.替换
//        href = href.replace("[", "");
//        href = href.replace("]", "");
        href = Tool.replaceKuiHao(href);
        String title = results[1];
//        title = title.replace("[", "");
//        title = title.replace("]", "");
        title = Tool.replaceKuiHao(title);
        String[] hrefs = {};
        hrefs = href.split(", ");
        String[] titles = title.split(", ");
        int length = hrefs.length;
        NavModel[] navModels = new NavModel[length];
        for (int i = 0; i < length; i++) {
            NavModel navModel = new NavModel();
            navModel.setTitle(titles[i]);
            navModel.setHref(hrefs[i]);
            navModels[i] = navModel;
        }
        return navModels;
    }
//    private String readData() {
//
//        StringBuilder result = new StringBuilder();
//        InputStream inputStream = null;
//        try {
//            inputStream = new FileInputStream(file);
//            Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
//            int read = 0;
//            while ((read = reader.read()) != -1) {
//                char c = (char) read;
//                result.append(c);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return result.toString();
//    }

//    public static void main(String[] args) {
//        new NavServiceImpl().getNavModels();
//    }

    /**
     *
     */
//    private void writeData() {
//        try {
//            OutputStream outputStream = new FileOutputStream(file);
//            NavDao navDao = new NavDao();
////            字符串可以直接转换位字节数组
////            需要将字符串数组转换位字节数组？
////            字符串数组---》字符串---》字节数组
////            空指针没有任何方法NullPointerException
//            int length = navDao.hrefs.length;
////            StringBuilder result = new StringBuilder();
////            for (int i = 0; i < length; i++) {
////                if (i == length - 1) {
////                    result.append(navDao.hrefs[i]);
////                } else {
////                    result.append(navDao.hrefs[i]).append(",");
////                }
////            }
////            System.out.println(result);
////            /home,/company,/news,
////            怎么去掉逗号？
////            可不可以用截取？
////   拿到字符串的某一个字符 result.charAt(3)
////            subString(3);
////            String temp=result.substring(0,11);
////            String temp = result.substring(0, result.length() - 1);
////            System.out.println(temp);
////            String[] aa=new String[0];
////            String [] aa=null;
//            String href = Arrays.toString(navDao.hrefs);
////            System.out.println(href);
//            outputStream.write(href.getBytes());
//            outputStream.write(split.getBytes(StandardCharsets.UTF_8));
//            outputStream.write(Arrays.toString(navDao.titles).getBytes());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

//    private void createFile() {
//        String path = "E:\\wanxi\\step-3\\canyin03-0913\\web";
//        String directory = "note";
//        String fileName = "nav.txt";
//        File fileDirecotry = new File(path, directory);
//        fileDirecotry.mkdirs();
//        File tempFile = new File(fileDirecotry, fileName);
//        try {
//            tempFile.createNewFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
////        变量的就近原则
//        file = tempFile;
//    }


}
